package com.example.java_second_hand_market;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.sql.*;

public class LoginDialog extends Stage {
    @FXML
    Label userLabel = new Label("用户名: ");

    @FXML
    TextField userName = new TextField();
    @FXML
    Label passLabel = new Label("密  码: ");
    @FXML
    PasswordField userPassword = new PasswordField();
    @FXML
    Label sfLabel = new Label("身份：");
    @FXML
    RadioButton BuyerRB = new RadioButton("买家");
    @FXML
    RadioButton SellerRB = new RadioButton("商家");
    @FXML
    RadioButton adminRB = new RadioButton("管理员");
    @FXML
    Button loginButton = new Button("登录", new ImageView());
    @FXML
    Button quitButton = new Button("退出", new ImageView());
    @FXML
    Button registerButton = new Button("注册",new ImageView());
    String userId;
    MainMarket appUser;

    String DBUrl=ParameterDB.ParaDBUserUrl;
    String DBUser="root", DBPassword="123456";
    public LoginDialog(MainMarket app, int width, int height){
        appUser = app;
        userName.setPrefWidth(150);
        userPassword.setPrefWidth(150);

        userLabel.setTextFill(Color.WHITE);
        passLabel.setTextFill(Color.WHITE);


        loginButton.setStyle("-fx-text-fill:red");
        loginButton.setOnAction(new EventHandler<ActionEvent>()
        {   public void handle(ActionEvent e)
        {   loginAction();
        }
        });

        quitButton.setOnAction(new EventHandler<ActionEvent>()
        {   public void handle(ActionEvent e)
        {	close();
            Platform.exit();
        }
        });
        registerButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                close();
                RegisterDialog rd = new RegisterDialog(appUser,width,height);

            }
        });

        VBox vb = new VBox();
        vb.setSpacing(10);

        HBox hb1 = new HBox();
        hb1.getChildren().addAll(userLabel, userName);
        hb1.setMargin(userLabel, new Insets(3,0,0,0));
        hb1.setMargin(userName, new Insets(0,0,0,3));

        HBox hb2 = new HBox();
        hb2.getChildren().addAll(passLabel, userPassword);
        hb2.setMargin(passLabel, new Insets(3,0,0,0));
        hb2.setMargin(userPassword, new Insets(0,0,0,6));

        HBox hb3 = new HBox();
        hb3.getChildren().addAll(sfLabel, BuyerRB, SellerRB, adminRB);
        hb3.setSpacing(5);
        hb3.setMargin(sfLabel, new Insets(2,0,0,0));
        hb3.setMargin(BuyerRB, new Insets(0,0,0,5));

        ToggleGroup tg = new ToggleGroup();
        BuyerRB.setToggleGroup(tg);
        SellerRB.setToggleGroup(tg);
        adminRB.setToggleGroup(tg);

        HBox hb4 = new HBox();
        hb4.setSpacing(20);
        hb4.setAlignment(Pos.CENTER);
        hb4.getChildren().addAll(loginButton, quitButton,registerButton);

        vb.getChildren().addAll(hb1, hb2, hb3);

        BorderPane bPane = new BorderPane();
        Image img= new Image("C:\\Users\\13561\\Desktop\\学习资料\\java\\picture\\background.jpg",300, 200, false, false);
        BackgroundImage bImg = new BackgroundImage(img, BackgroundRepeat.NO_REPEAT,
                BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
        bPane.setBackground(new Background(bImg));
        bPane.setCenter(vb);
        bPane.setBottom(hb4);
        bPane.setPadding(new Insets(15,40,20,40));

        setX(820);
        setY(500);
        initModality(Modality.APPLICATION_MODAL);
        setScene(new Scene(bPane));


        setResizable(false);
        setWidth(300);
        setHeight(200);
        setX((width-getWidth()) / 2);
        setY((height-getHeight())/ 2);
        setTitle("登录系统");
        show();
    }   //构造函数
    public void loginAction(){
        userId = (userName.getText()).trim();
        String pw = (userPassword.getText()).trim();
        try
        {   Connection con = DriverManager.getConnection(DBUrl, DBUser, DBPassword);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM user_list where account = '" + userId + "'");
            if(rs.next())
            {   String pwStr = rs.getString("password");
                String acStr = rs.getString("account");
                String nameStr = rs.getString("name").trim();
                if((pwStr==null || pwStr.equals(pw))&&!rs.getBoolean("is_ban"))
                {
                    if(userId.length() == 0)
                    {   CommonDialog.WarningDialog("请输入用户名！");
                        return;
                    }
                    if(BuyerRB.isSelected())
                    {
                        appUser.loginasbuyer(nameStr,acStr,pwStr);
                    }
                    if(SellerRB.isSelected())
                    {   appUser.loginasseller(nameStr,acStr,pwStr);
                    }
                    if(adminRB.isSelected()&&rs.getBoolean("is_admin"))
                    {   appUser.loginasadmin(nameStr,acStr,pwStr);
                    }



                    if(con!=null) {
                        try {
                            rs.close();
                            stmt.close();
                            con.close();

                        }catch (SQLException err){
                            System.out.println("112131");
                        }
                        close();
                    }
                }
                else
                    if(!rs.getBoolean("is_ban")) {
                        CommonDialog.WarningDialog("密码不正确， 请重新输入！");
                    }
                    else {
                        CommonDialog.WarningDialog("你已被封禁！");
                    }
            }
            else
                CommonDialog.WarningDialog("《"+userId+"》不存在， 请重新输入！");
            if(con!=null) {
                try {
                    rs.close();
                    stmt.close();
                    con.close();

                }catch (SQLException err){
                    System.out.println("1wew");
                }
            }
        }
        catch(SQLException s)
        {   System.out.println("不能打开 management， 数据库操作失败！");
        }




    }    //登录事件函数
}
